package com.weiai.wedding.dao.cases;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Expression;
import javax.persistence.criteria.Order;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.hibernate.ejb.criteria.OrderImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.weiai.wedding.dao.BaseQueryDao;
import com.weiai.wedding.entity.cases.Cases;
import com.weiai.wedding.util.Pagination;

@Component
public class CasesExpDaoImpl implements CasesExpDao{
	
	@PersistenceContext 
	private EntityManager em;
	
	@Autowired
	private BaseQueryDao base;
	@Override
	public Pagination<Cases> findList(String casesName, String casesTitle,Pagination page) {
		CriteriaBuilder builder = em.getCriteriaBuilder(); 
		CriteriaQuery<Cases> query = builder.createQuery(Cases.class); 
		Root<Cases> cases = query.from(Cases.class); 
		query.select(cases);
		
		List<Predicate> predicateList = new ArrayList<Predicate>(); 
		Predicate casesBatchNumPredicate; 

		Expression<String> mn = cases.get("casesName");
		Expression<String> mt = cases.get("casesTitle");
		Expression<Long> id = cases.get("casesId");
		if ((casesName != null) && (!(casesName.isEmpty()))) { 
			casesBatchNumPredicate = builder.like(mn,"%"+casesName+"%");
			predicateList.add(casesBatchNumPredicate); 
		} 
		if ((casesTitle != null) && (!(casesTitle.isEmpty()))) { 
			casesBatchNumPredicate = builder.like(mt, 
					"%"+casesTitle+"%");
			predicateList.add(casesBatchNumPredicate); 
		}
		casesBatchNumPredicate = builder.notEqual(cases.get("status"), "X");
		predicateList.add(casesBatchNumPredicate);
		Predicate[] predicates = new Predicate[predicateList.size()]; 
		predicateList.toArray(predicates); 
		query.where(predicates); 
		Order desc = builder.desc(cases.get("casesId"));
		query.orderBy(desc);
		Pagination pg = base.getPage(query, page, Cases.class, predicates);
		
		return pg; 

	}
	
	public Pagination<Cases> findListByStatus(String casesName, String casesTitle,Pagination page) {
		CriteriaBuilder builder = em.getCriteriaBuilder(); 
		CriteriaQuery<Cases> query = builder.createQuery(Cases.class); 
		Root<Cases> cases = query.from(Cases.class); 
		query.select(cases);
		
		List<Predicate> predicateList = new ArrayList<Predicate>(); 
		Predicate casesBatchNumPredicate; 

		Expression<String> mn = cases.get("casesName");
		Expression<String> mt = cases.get("casesTitle");
		Expression<Long> id = cases.get("casesId");
		if ((casesName != null) && (!(casesName.isEmpty()))) { 
			casesBatchNumPredicate = builder.like(mn,"%"+casesName+"%");
			predicateList.add(casesBatchNumPredicate); 
		} 
		if ((casesTitle != null) && (!(casesTitle.isEmpty()))) { 
			casesBatchNumPredicate = builder.like(mt, 
					"%"+casesTitle+"%");
			predicateList.add(casesBatchNumPredicate); 
		}
		casesBatchNumPredicate = builder.equal(cases.get("status"), "A");
		predicateList.add(casesBatchNumPredicate);
		Predicate[] predicates = new Predicate[predicateList.size()]; 
		predicateList.toArray(predicates); 
		query.where(predicates); 
		Query createQuery = em.createQuery(query);
		Order desc = builder.desc(cases.get("casesId"));
		query.orderBy(desc);
		Pagination pg = base.getPage(query, page, Cases.class, predicates);
		
		return pg; 

	}
	
	@Override
	public Pagination<Cases> findCasesList(String casesName, String casesTitle,Pagination page) {
		CriteriaBuilder builder = em.getCriteriaBuilder(); 
		CriteriaQuery<Cases> query = builder.createQuery(Cases.class); 
		Root<Cases> cases = query.from(Cases.class); 
		query.select(cases);
		
		List<Predicate> predicateList = new ArrayList<Predicate>(); 
		Predicate casesBatchNumPredicate; 

		Expression<String> mn = cases.get("casesName");
		Expression<String> mt = cases.get("casesTitle");
		Expression<Long> id = cases.get("casesId");
		if ((casesName != null) && (!(casesName.isEmpty()))) { 
			casesBatchNumPredicate = builder.like(mn,"%"+casesName+"%");
			predicateList.add(casesBatchNumPredicate); 
		} 
		if ((casesTitle != null) && (!(casesTitle.isEmpty()))) { 
			casesBatchNumPredicate = builder.like(mt, 
					"%"+casesTitle+"%");
			predicateList.add(casesBatchNumPredicate); 
		}
		casesBatchNumPredicate = builder.equal(cases.get("status"), "A");
		predicateList.add(casesBatchNumPredicate);
		Predicate[] predicates = new Predicate[predicateList.size()]; 
		predicateList.toArray(predicates); 
		query.where(predicates); 
		Query createQuery = em.createQuery(query);
		Order desc = builder.desc(cases.get("casesId"));
		query.orderBy(desc);
		Pagination pg = base.getPage(query, page, Cases.class, predicates);
		
		return pg; 

	}

}
